package com.uenpay.im.ui.old.activity.funds;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.qiyunxin.android.http.HttpInvokeResultCode;
import com.qiyunxin.android.http.utils.Utils;
import com.uenpay.im.IMApplication;
import com.uenpay.im.R;
import com.uenpay.util.app.base.BaseActivity;

/**
 * @author SL
 * @version 1.0
 * @copyRight http://www.qiyunxin.com
 * @E-mail:
 * @date 创建时间：2016-5-9 下午3:57:49
 * @function 收取经费
 */
public class ReceiveFundsActivity extends BaseActivity {

    private Button comfirm_btn;
    private EditText amount_et;
    private View loading;
    private String chatId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        isFullScreen = true;
        super.onCreate(savedInstanceState);
    }

    @Override
    public View bindView() {
        return null;
    }

    @Override
    public int bindLayout() {
        return R.layout.activity_receive_funds_layout;
    }

    @Override
    public void initView(View view) {
        chatId = getIntent().getStringExtra("chat_id");
        initView();
        initListener();
    }

    protected void initView() {
        ((TextView) findViewById(R.id.title_textview))
                .setText(R.string.received_funds);
        comfirm_btn = (Button) findViewById(R.id.comfirm_btn);
        amount_et = (EditText) findViewById(R.id.amount_et);
        loading = findViewById(R.id.loading);
    }

    protected void initListener() {
        findViewById(R.id.back_layout).setOnClickListener(
                new OnClickListener() {

                    @Override
                    public void onClick(View arg0) {
                        Utils.hideSoftKeyboard(amount_et);
                        finish();
                    }
                });
        amount_et.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence arg0, int arg1, int arg2,
                                      int arg3) {
                checkBtnStatus();
            }

            @Override
            public void beforeTextChanged(CharSequence arg0, int arg1,
                                          int arg2, int arg3) {
            }

            @Override
            public void afterTextChanged(Editable arg0) {
            }
        });

        comfirm_btn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                String amount = amount_et.getText().toString();
                if (!TextUtils.isEmpty(amount)) {
                    addFunds(amount);
                }
            }
        });
    }

    private void checkBtnStatus() {
        String amount = amount_et.getText().toString();
        if (!TextUtils.isEmpty(amount)) {
            comfirm_btn.setBackgroundResource(R.drawable.button_bg_normal);
            comfirm_btn.setEnabled(true);
        } else {
            comfirm_btn.setEnabled(false);
            comfirm_btn.setBackgroundResource(R.drawable.button_bg_gray);
        }
    }

    private void addFunds(String amount) {
        loading.setVisibility(View.VISIBLE);
        FundsHandle.deliveryFunds(chatId, amount, new FundsHandle.IFundsListener() {

            @Override
            public void onFundsResult(int status, String msg) {
                loading.setVisibility(View.GONE);
                if (status == HttpInvokeResultCode.RESULT_OK) {
                    setResult(RESULT_OK);
                    finish();
                } else if (!TextUtils.isEmpty(msg)) {
                    IMApplication.showToast(msg);
                }
            }
        });
    }

    @Override
    public void onBackPressed() {
        finish();
        super.onBackPressed();
    }
}
